import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken, setLogintime ,getLogintime,removeLogintime} from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    login_time:'',
    name: '',
    avatar: '',
    nickName:"",
    roles: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_LOGIN_TIME: (state) => {
      state.login_time = new Date().getTime();
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_NICKNAME: (state, nickName) => {
      state.nickName = nickName
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          const data = response.data
          const tokenStr = data.tokenHead+data.token
          setToken(tokenStr);
          setLogintime(new Date().getTime());
          commit('SET_TOKEN', tokenStr)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      let nowTime = new Date().getTime();
      let lastLoginTime = getLogintime();
      let interval = 12*60*60*1000;
      if(lastLoginTime && nowTime-lastLoginTime>interval){//登录时间超过12小时
        removeLogintime();
        removeToken();
        location.reload() // 为了重新实例化vue-router对象 避免bug
        this.$router.push({path:'/login'})
        return
      }
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const data = response.data;
          console.log(data);
          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            //commit('SET_ROLES', data.roles)
            const testRoles=['admin','test']
            commit('SET_ROLES', testRoles);
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          commit('SET_NAME', data.username)
          commit('SET_AVATAR', data.icon)
          commit('SET_NICKNAME', data.nickName)
          console.log(data.nickName);
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken();
          removeLogintime();
          resolve()
        // logout(state.token).then(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_ROLES', [])
        //   removeToken()
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
